using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Deepcode.Flux.Core.Data;

namespace Deepcode.Flux.Core.Systems.Addins
{
	/// <summary>
	/// Manager class for the known addin records residing in the UpdateAddins table
	/// </summary>
	public class KnownAddins : BaseManager
	{
		#region Constructors
		public KnownAddins() : base() { }
		public KnownAddins(SqlConnection CurrentConnection) : base(CurrentConnection) { }
		public KnownAddins(SqlConnection CurrentConnection, SqlTransaction CurrentTransaction) : base(CurrentConnection, CurrentTransaction) { }
		#endregion
		#region Data Loaders
		/// <summary>
		/// Loads a KnownAddin record by it's guid.
		/// </summary>
		/// <param name="guid"></param>
		/// <returns></returns>
		public KnownAddin GetByGuid(System.Guid guid)
		{
			string dbGuid = guid.ToString();
			DataTable t = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreKnownAddinGetByGuid");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@GUID", SqlDbType.NVarChar, 150, dbGuid));
			da.Fill(t);
			ReleaseConnection();

			if (t == null) return null;
			if (t.Rows.Count < 1) return null;

			KnownAddin result = new KnownAddin(t.Rows[0]);
			return result;
		}
		#endregion
		#region Version Getters/Setters
		/// <summary>
		/// Returns a system.version for the known version of the 
		/// addin in the database. If the addin isn't found, the 
		/// version returned will be 0.0.0.0
		/// </summary>
		/// <param name="guid"></param>
		/// <returns></returns>
		public System.Version GetDbVersionFor(System.Guid guid)
		{
			KnownAddin a = GetByGuid(guid);
			if (a == null) return new Version(0, 0, 0, 0);
			return new Version(a.VersionString);
		}
		/// <summary>
		/// Sets the version of the addin in the database. If the addin couldn't be found, it is created.
		/// </summary>
		/// <param name="guid"></param>
		/// <param name="newversion"></param>
		public void SetDbVersionFor(Guid guid, string newversion)
		{
			try
			{
				EstablishConnection();
				BeginTransaction();

				KnownAddin a = GetByGuid(guid);
				if (a == null)
				{
					a = new KnownAddin();
					a.GUID = guid;
					a.AddinName = "Unknown";
				}
				a.VersionString = newversion;
				Save(a);

				EndTransaction(EndTransactionMode.COMMIT);
				ReleaseConnection();
			}
			catch (Exception e)
			{
				EndTransaction(EndTransactionMode.ROLLBACK);
				ReleaseConnection();
				throw e;
			}
		}
		#endregion
		#region Record Save Operations
		/// <summary>
		/// Save the record, specifying the connection and transaction to use
		/// </summary>
		/// <param name="c"></param>
		/// <param name="t"></param>
		/// <returns></returns>
		public int Save(KnownAddin data)
		{
			try
			{
				// Establish the connection if necessary
				EstablishConnection();
				// Create the command
				SqlCommand sq = CreateSqlCommand("coreKnownAddInSave");
				// Add the in/out parameter for PK_ID		
				SqlParameter result = SQLUtility.ParameterInOut("@PK_ID", SqlDbType.Int, 4, data.PK_ID);
				sq.Parameters.Add(result);
				// Add other parameters
				sq.Parameters.Add(SQLUtility.ParameterIn("@GUID", SqlDbType.NVarChar, 150, data.dbGUID));
				sq.Parameters.Add(SQLUtility.ParameterIn("@AddinName", SqlDbType.NVarChar, 200, data.AddinName));
				sq.Parameters.Add(SQLUtility.ParameterIn("@VersionString", SqlDbType.NVarChar, 50, data.VersionString));
				// Execute it
				sq.ExecuteNonQuery();
				// Release the connection if necessary
				this.ReleaseConnection();
				// Read the return value
				if (data.PK_ID == -1) data.PK_ID = (int)result.Value;
				return data.PK_ID;
			}
			catch (Exception e)
			{
				// Close the connection if we opened it
				this.ReleaseConnection();
				throw e;
			}
		}
		#endregion
	}
}
